/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */

package com.tech15.task.service;

import java.io.File;
import java.util.List;

import javacommon.base.BaseManager;
import javacommon.base.EntityDao;
import javacommon.util.DateTools;
import javacommon.util.Notes;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;
import cn.org.rapid_framework.util.ObjectUtils;

import com.tech15.core.dao.SysUserDao;
import com.tech15.core.dao.SysYgDao;
import com.tech15.core.model.Attach;
import com.tech15.core.model.SysUser;
import com.tech15.core.model.SysYg;
import com.tech15.core.service.AttachManager;
import com.tech15.pub.dao.MsgTipsDao;
import com.tech15.pub.dao.PubDataDao;
import com.tech15.pub.model.MsgTips;
import com.tech15.pub.model.PubData;
import com.tech15.task.dao.TaskAuthDao;
import com.tech15.task.dao.TaskDao;
import com.tech15.task.dao.TaskDeptDao;
import com.tech15.task.dao.TaskProcessDao;
import com.tech15.task.dao.TaskReturnDao;
import com.tech15.task.model.Task;
import com.tech15.task.model.TaskAuth;
import com.tech15.task.model.TaskDept;
import com.tech15.task.model.TaskProcess;
import com.tech15.task.model.TaskReturn;
import com.tech15.task.vo.query.TaskQuery;

/**
 * @author 胡作正 email:zuozhenghu@sina.com
 * @version 1.0
 * @since 1.0
 */

@Service
@Transactional
public class TaskManager extends BaseManager<Task,Long>{

	private TaskDao taskDao;
	private AttachManager attachManager;
	private TaskProcessDao taskProcessDao;
	private SysUserDao sysUserDao;
	private MsgTipsDao msgTipsDao;
	private TaskAuthDao taskAuthDao;
	private TaskDeptDao taskDeptDao;
	private TaskReturnDao taskReturnDao;
	private SysYgDao sysYgDao;
	private PubDataDao pubDataDao;
	
	public void setPubDataDao(PubDataDao pubDataDao) {
		this.pubDataDao = pubDataDao;
	}
	public void setSysYgDao(SysYgDao sysYgDao) {
		this.sysYgDao = sysYgDao;
	}
	public void setTaskReturnDao(TaskReturnDao taskReturnDao) {
		this.taskReturnDao = taskReturnDao;
	}
	public void setTaskDeptDao(TaskDeptDao taskDeptDao) {
		this.taskDeptDao = taskDeptDao;
	}
	public void setTaskAuthDao(TaskAuthDao taskAuthDao) {
		this.taskAuthDao = taskAuthDao;
	}
	public void setMsgTipsDao(MsgTipsDao dao) {
		this.msgTipsDao = dao;
	}
	public void setSysUserDao(SysUserDao dao) {
		this.sysUserDao = dao;
	}
	
	public void setTaskProcessDao(TaskProcessDao dao) {
		this.taskProcessDao = dao;
	}
	
	public void setAttachManager(AttachManager attachManager) {
		this.attachManager = attachManager;
	}
	
	
	public void setTaskDao(TaskDao dao) {
		this.taskDao = dao;
	}
	public EntityDao getEntityDao() {
		return this.taskDao;
	}
	
	@Transactional(readOnly=true)
	public Page findPage(TaskQuery query) {
		return taskDao.findPage(query);
	}
	
	@Transactional(readOnly=true)
	public Page findByPageRequest(PageRequest pr) {
		return taskDao.findByPageRequest(pr);
	}
	
	public Task getById(Long taskid){
		Task task = taskDao.getById(taskid);
		
		if(ObjectUtils.isNotEmpty(task.getCreater())){
			SysUser user = sysUserDao.getById(task.getCreater());
			if(user != null){
				task.setUsername(user.getRealname());
				SysYg yg = sysYgDao.getById(user.getUsername());
				if(yg != null){
					task.setTel(yg.getTel());
				}
			}
		}
		
		if(ObjectUtils.isNotEmpty(task.getThr())){
			SysUser user = sysUserDao.getById(task.getThr());
			if(user != null){
				task.setThrxm(user.getRealname());
			}
		}
		
		if(ObjectUtils.isNotEmpty(task.getLb())){
			PubData pd = pubDataDao.getById(task.getLb());
			if(pd != null){
				task.setDataname(pd.getName());
			}
		}
		
		List<TaskProcess> list = taskProcessDao.findByTaskid(taskid);
		task.setTaskProcessList(list);
		/*int spindex = 0;
		for(TaskProcess tp : list){
			if(tp.getOpttype() == 1){
				if(spindex == 0){
					task.setShrxm(tp.getUsername());
				}else{
					task.setSprxm(tp.getUsername());
				}
				spindex++;
			}
		}
		if(spindex == 0 || spindex == 1){
			task.setSprxm(task.getShrxm());
		}*/
		
		List<TaskDept> taskdepts = taskDeptDao.searchTaskDept(taskid,1);
		task.setTaskdepts(taskdepts);
		
		List<TaskDept> taskSendDepts = taskDeptDao.searchTaskDept(taskid,2);
		task.setTaskSendDepts(taskSendDepts);
		
		/*获取相关附件*/
		List<Attach> attach = attachManager.findByTableNameAndId(Notes.task, ""+taskid);
		task.setAttach(attach);
		
		/*if(task.getTaskstate() == 0){
			Integer spcs = taskDao.getCount("select count(*) from task_process t where t.opttype=1 and t.taskid="+taskid);
			task.setSpcs(spcs);
		}*/
		return task;
	}
	
	/**新建任务*/
	public void saveTask(Task task, String realpath, List<File> attachFile, List<String> attachFileFileName, List<String> attachFileContentType) {
		String rwbh = taskDao.searchMaxRwbh();
		task.setSerial(rwbh);
		task.setCreattime(DateTools.getCurrentTime());
		task.setTaskstate(0);
		task.setExecstate(-1);
		taskDao.save(task);
		taskDao.flush();
		
		//保存新任务
		TaskProcess tp = new TaskProcess();
		tp.setTaskid(task.getTaskid());
		tp.setTasktime(DateTools.getCurrentTime());
//		判断是否有代理人
		/*TaskAuth taskAuth = taskAuthDao.getById(task.getHanlder());
		if(taskAuth != null){
			tp.setHanlder(taskAuth.getAuthid());
			tp.setHanldetype(3);
		}else{
			tp.setHanlder(task.getHanlder());
			tp.setHanldetype(1);
		}*/
		tp.setZpr(task.getCreater());
		if(task.getLb() == 160){
			tp.setHanlder(task.getXmspr());
		}else{
			tp.setHanlder(task.getHanlder());
		}
		tp.setHanldetype(1);
		tp.setOpttype(task.getOpttype());
		tp.setHanldestate(0);
		tp.setTaskaction(1);
		taskProcessDao.save(tp);
		
		if(task.getDeptids() != null){
			for(Long deptid : task.getDeptids()){
				TaskDept td = new TaskDept();
				td.setTaskid(task.getTaskid());
				td.setDeptid(deptid);
				td.setSendtype(1);
				taskDeptDao.save(td);
			}
		}
		
		if(task.getSendDeptids() != null){
			for(Long deptid : task.getSendDeptids()){
				TaskDept td = new TaskDept();
				td.setTaskid(task.getTaskid());
				td.setDeptid(deptid);
				td.setSendtype(2);
				taskDeptDao.save(td);
			}
		}
		
		//保存附件
		if(attachFile != null && attachFileFileName != null){
			attachManager.saveAttachs(realpath, task.getCreater(),"task", ""+task.getTaskid(), attachFile, attachFileFileName, attachFileContentType);
		}
		
		//保存任务提示
		MsgTips tips = new MsgTips();
		tips.setBt(task.getTitle());
		tips.setContent(task.getTitle());
		tips.setTablename(Notes.task);
		tips.setTableid(task.getTaskid()+"");
		tips.setZt(0);
		tips.setXxsj(DateTools.getCurrentTime());
		/*if(taskAuth != null){
			tips.setUserid(taskAuth.getAuthid());
		}else{
			tips.setUserid(task.getHanlder());
		}*/
		tips.setUserid(task.getHanlder());
		tips.setUrl(Notes.taskUrl+"taskid="+task.getTaskid());
		msgTipsDao.save(tips);
		
	}
	
	/**退回任务*/
	public void thTask(Task task){
		TaskProcess tp =  taskProcessDao.getById(task.getTpid());
		tp.setHanldresult(task.getHanldresult());
		tp.setHanldtime(DateTools.getCurrentTime());
		tp.setHanldestate(1);
		tp.setOpttype(-1);
		taskProcessDao.update(tp);
	}
	
	@Transactional(readOnly=true)
	public Page myTask(TaskQuery query) {
		return taskDao.myTask(query);
	}
	/**转下一审批*/
	public void zxyspTask(Task task, String realpath, List<File> attachFiles, List<String> attachFilesFileName, List<String> attachFilesContentType) {
		task.setTaskstate(0);
		taskDao.update(task);
		
		TaskProcess tp =  taskProcessDao.getById(task.getTpid());
		tp.setHanldresult(task.getHanldresult());
		tp.setHanldtime(DateTools.getCurrentTime());
		tp.setHanldestate(1);
		tp.setHanldetype(4);//设置办理类型为“转下一审批”
		taskProcessDao.update(tp);
		
//		保存新任务
		TaskProcess nextTp = new TaskProcess();
		nextTp.setTaskid(task.getTaskid());
		nextTp.setTasktime(DateTools.getCurrentTime());
		/*//判断是否有代理人
		TaskAuth taskAuth = taskAuthDao.getById(task.getHanlder());
		if(taskAuth != null){
			nextTp.setHanlder(taskAuth.getAuthid());
			nextTp.setHanldetype(3);
		}else{
			nextTp.setHanlder(task.getHanlder());
			nextTp.setHanldetype(1);
		}*/
		nextTp.setZpr(tp.getHanlder());
		nextTp.setHanlder(task.getHanlder());
		nextTp.setHanldetype(1);
		nextTp.setOpttype(task.getOpttype());
		nextTp.setHanldestate(0);
		nextTp.setTaskaction(1);
		taskProcessDao.save(nextTp);
		
		attachManager.saveAttachs(realpath, tp.getHanlder(),"task", ""+task.getTaskid(), attachFiles, attachFilesFileName, attachFilesContentType);
		
		/**保存任务提示*/
		MsgTips tips = new MsgTips();
		tips.setBt(task.getTitle());
		tips.setContent(task.getTitle());
		tips.setTablename(Notes.task);
		tips.setTableid(task.getTaskid()+"");
		tips.setZt(0);
		tips.setXxsj(DateTools.getCurrentTime());
		/*if(taskAuth != null){
			tips.setUserid(taskAuth.getAuthid());
		}else{
			tips.setUserid(task.getHanlder());
		}*/
		tips.setUserid(task.getHanlder());
		tips.setUrl(Notes.taskUrl+"taskid="+task.getTaskid());
		msgTipsDao.save(tips);
	}
	/**审批完成*/
	public Integer spwcTask(Task task, String realpath, List<File> attachFiles, List<String> attachFilesFileName, List<String> attachFilesContentType) {
		TaskProcess tp =  taskProcessDao.getById(task.getTpid());
		
		if("同意".equals(task.getHanldresult())){
			Integer bmws = taskDeptDao.getCount("select count(*) from task_dept t where t.sendtype=1 and t.taskid="+task.getTaskid());
			List<SysUser> listDeptUser = taskDeptDao.searchTaskUserDept(task.getTaskid(),1);
			if(listDeptUser == null){
				return 0;
			}else if(bmws == 0){
				return 0;
			}else if(bmws != listDeptUser.size()){
				return 2;
			}else{
				for(SysUser user : listDeptUser){
					TaskProcess nextTp = new TaskProcess();
					nextTp.setTaskid(task.getTaskid());
					nextTp.setTasktime(DateTools.getCurrentTime());
					nextTp.setHanlder(user.getUserid());
					nextTp.setHanldetype(1);
					nextTp.setOpttype(task.getOpttype());
					nextTp.setHanldestate(0);
					nextTp.setTaskaction(-1);
					nextTp.setZpr(tp.getHanlder());
					taskProcessDao.save(nextTp);
					
					
					MsgTips tips = new MsgTips();
					tips.setBt(task.getTitle());
					tips.setContent(task.getTitle());
					tips.setTablename(Notes.task);
					tips.setTableid(task.getTaskid()+"");
					tips.setZt(0);
					tips.setUserid(user.getUserid());
					tips.setXxsj(DateTools.getCurrentTime());
					tips.setUrl(Notes.taskUrl+"taskid="+task.getTaskid());
					msgTipsDao.save(tips);
					
				}
			}
			
			task.setTaskstate(1);
			task.setExecstate(0);
			task.setAuthtime(DateTools.getCurrentTime());
			
			tp.setHanldresult(task.getHanldresult());
			tp.setHanldtime(DateTools.getCurrentTime());
			tp.setHanldestate(1);
			tp.setHanldetype(5);//设置办理类型为“审批完成”
		}else{
			task.setTaskstate(2);
			task.setThr(tp.getHanlder());
			task.setAuthtime(DateTools.getCurrentTime());
			taskDao.update(task);
			
			TaskProcess nextTp = new TaskProcess();
			nextTp.setTaskid(task.getTaskid());
			nextTp.setTasktime(DateTools.getCurrentTime());
			nextTp.setHanlder(task.getCreater());
			nextTp.setHanldetype(1);
			nextTp.setOpttype(-1);
			nextTp.setHanldestate(0);
			nextTp.setTaskaction(0);
			nextTp.setZpr(tp.getHanlder());
			taskProcessDao.save(nextTp);
			
			MsgTips tips = new MsgTips();
			tips.setBt(task.getTitle());
			tips.setContent(task.getTitle());
			tips.setTablename(Notes.task);
			tips.setTableid(task.getTaskid()+"");
			tips.setZt(0);
			tips.setUserid(task.getCreater());
			tips.setXxsj(DateTools.getCurrentTime());
			tips.setUrl(Notes.taskUrl+"taskid="+task.getTaskid());
			msgTipsDao.save(tips);
			
			if("退回".equals(task.getHanldresult())){
				tp.setHanldresult(task.getHanldresult());
				tp.setHanldtime(DateTools.getCurrentTime());
				tp.setHanldestate(1);
				tp.setHanldetype(6);
			}
		}
		
		if(attachFiles != null && attachFilesFileName != null){
			attachManager.saveAttachs(realpath, tp.getHanlder(),"task", ""+task.getTaskid(), attachFiles, attachFilesFileName, attachFilesContentType);
		}
		
		taskProcessDao.update(tp);
		taskDao.update(task);
		
		return 1;
	}
	
	/**审核任务*/
	public void tjshTask(Task task, String realpath, List<File> attachFile, List<String> attachFileFileName, List<String> attachFileContentType) {
		TaskProcess tp =  taskProcessDao.getById(task.getTpid());
		tp.setHanldresult(task.getHanldresult());
		tp.setHanldtime(DateTools.getCurrentTime());
		tp.setHanldestate(1);
		taskProcessDao.update(tp);
		
		TaskProcess nextTp = new TaskProcess();
		nextTp.setTaskid(task.getTaskid());
		nextTp.setTasktime(DateTools.getCurrentTime());
		nextTp.setHanlder(task.getHanlder());
		nextTp.setHanldetype(1);
		nextTp.setOpttype(task.getOpttype());
		nextTp.setHanldestate(0);
		nextTp.setTaskaction(1);
		nextTp.setZpr(tp.getHanlder());
		taskProcessDao.save(nextTp);
		
		
		MsgTips tips = new MsgTips();
		tips.setBt(task.getTitle());
		tips.setContent(task.getTitle());
		tips.setTablename(Notes.task);
		tips.setTableid(task.getTaskid()+"");
		tips.setZt(0);
		tips.setUserid(task.getHanlder());
		tips.setXxsj(DateTools.getCurrentTime());
		tips.setUrl(Notes.taskUrl+"taskid="+task.getTaskid());
		msgTipsDao.save(tips);
		
		if(attachFile != null && attachFileFileName != null){
			attachManager.saveAttachs(realpath, tp.getHanlder(),"task", ""+task.getTaskid(), attachFile, attachFileFileName, attachFileContentType);
		}
		
	}
	
	/**审核任务*/
	public int shTask(Task task, String realpath, List<File> attachFile, List<String> attachFileFileName, List<String> attachFileContentType) {
		TaskProcess tp =  taskProcessDao.getById(task.getTpid());
		tp.setHanldresult(task.getHanldresult() + task.getOptHanldresult());
		tp.setHanldtime(DateTools.getCurrentTime());
		tp.setHanldestate(1);
		taskProcessDao.update(tp);
		
		
		if("同意".equals(task.getHanldresult())){
			/*TaskProcess nextTp = new TaskProcess();
			nextTp.setTaskid(task.getTaskid());
			nextTp.setTasktime(DateTools.getCurrentTime());
			nextTp.setHanlder(task.getHanlder());
			nextTp.setHanldetype(1);
			nextTp.setOpttype(task.getOpttype());
			nextTp.setHanldestate(0);
			nextTp.setTaskaction(-2);
			taskProcessDao.save(nextTp);
			
			MsgTips tips = new MsgTips();
			tips.setBt(task.getTitle());
			tips.setContent(task.getTitle());
			tips.setTablename(Notes.task);
			tips.setTableid(task.getTaskid()+"");
			tips.setZt(0);
			tips.setUserid(task.getHanlder());
			tips.setXxsj(DateTools.getCurrentTime());
			tips.setUrl(Notes.taskUrl+"taskid="+task.getTaskid());
			msgTipsDao.save(tips);*/
			
			if(task.getExecUsers() != null){
				for(Long userid : task.getExecUsers()){
					TaskProcess nextTp = new TaskProcess();
					nextTp.setTaskid(task.getTaskid());
					nextTp.setTasktime(DateTools.getCurrentTime());
					nextTp.setHanlder(userid);
					nextTp.setHanldetype(1);
					nextTp.setOpttype(task.getOpttype());
					nextTp.setHanldestate(0);
					nextTp.setTaskaction(0);
					nextTp.setZpr(tp.getHanlder());
					taskProcessDao.save(nextTp);
					
					MsgTips tips = new MsgTips();
					tips.setBt(task.getTitle());
					tips.setContent(task.getTitle());
					tips.setTablename(Notes.task);
					tips.setTableid(task.getTaskid()+"");
					tips.setZt(0);
					tips.setXxsj(DateTools.getCurrentTime());
					tips.setUserid(userid);
					tips.setUrl(Notes.taskUrl+"taskid="+task.getTaskid());
					msgTipsDao.save(tips);
				}
			}
			
		}else{
			TaskReturn tr = new TaskReturn();
			tr.setTaskid(task.getTaskid());
			tr.setTpid(tp.getTpid());
			tr.setThr(tp.getHanlder());
			tr.setThsj(DateTools.getCurrentTime());
			tr.setZt(-1);
			taskReturnDao.save(tr);
			taskReturnDao.flush();
			task.setExecstate(2);
			
			MsgTips tips = new MsgTips();
			tips.setBt(task.getTitle());
			tips.setContent(task.getTitle());
			tips.setTablename(Notes.taskReturn);
			tips.setTableid(task.getTaskid()+"");
			tips.setZt(0);
			tips.setXxsj(DateTools.getCurrentTime());
			tips.setUserid(task.getCreater());
			tips.setUrl(Notes.taskReturnUrl+"trid="+tr.getTrid());
			msgTipsDao.save(tips);
		}
		
		taskDao.update(task);
		
		if(attachFile != null && attachFileFileName != null){
			attachManager.saveAttachs(realpath, tp.getHanlder(),"task", ""+task.getTaskid(), attachFile, attachFileFileName, attachFileContentType);
		}
		return 1;
	}
	
	/**执行分配任务*/
	public void zxfpTask(Task task, String realpath, List<File> attachFiles, List<String> attachFilesFileName, List<String> attachFilesContentType) {
		TaskProcess tp =  taskProcessDao.getById(task.getTpid());
		tp.setHanldresult(task.getHanldresult());
		tp.setHanldtime(DateTools.getCurrentTime());
		tp.setHanldestate(1);
		taskProcessDao.update(tp);
		
		task.setTaskstate(1);
		task.setExecstate(0);
		taskDao.update(task);
		
		if(attachFiles != null && attachFilesFileName != null){
			attachManager.saveAttachs(realpath, tp.getHanlder(),"task", ""+task.getTaskid(), attachFiles, attachFilesFileName, attachFilesContentType);
		}
		
		if(task.getExecUsers() != null){
			for(Long userid : task.getExecUsers()){
				TaskProcess nextTp = new TaskProcess();
				nextTp.setTaskid(task.getTaskid());
				nextTp.setTasktime(DateTools.getCurrentTime());
				nextTp.setHanlder(userid);
				nextTp.setHanldetype(1);
				nextTp.setOpttype(task.getOpttype());
				nextTp.setHanldestate(0);
				nextTp.setTaskaction(0);
				nextTp.setZpr(tp.getHanlder());
				taskProcessDao.save(nextTp);
				
				MsgTips tips = new MsgTips();
				tips.setBt(task.getTitle());
				tips.setContent(task.getTitle());
				tips.setTablename(Notes.task);
				tips.setTableid(task.getTaskid()+"");
				tips.setZt(0);
				tips.setXxsj(DateTools.getCurrentTime());
				tips.setUserid(userid);
				tips.setUrl(Notes.taskUrl+"taskid="+task.getTaskid());
				msgTipsDao.save(tips);
			}
		}
		
		/*
		TaskProcess nextTp = new TaskProcess();
		nextTp.setTaskid(task.getTaskid());
		nextTp.setTasktime(DateTools.getCurrentTime());
		nextTp.setHanlder(task.getHanlder());
		nextTp.setHanldetype(1);
		nextTp.setOpttype(task.getOpttype());
		nextTp.setHanldestate(0);
		nextTp.setTaskaction(0);
		taskProcessDao.save(nextTp);
		
		MsgTips tips = new MsgTips();
		tips.setBt(task.getTitle());
		tips.setContent(task.getTitle());
		tips.setTablename(Notes.task);
		tips.setTableid(task.getTaskid()+"");
		tips.setZt(0);
		tips.setXxsj(DateTools.getCurrentTime());
		tips.setUserid(task.getHanlder());
		tips.setUrl(Notes.taskUrl+"taskid="+task.getTaskid());
		msgTipsDao.save(tips);*/
		
	}
	
	/**执行转他人任务*/
	public void zxztrTask(Task task, String realpath, List<File> attachFile, List<String> attachFileFileName, List<String> attachFileContentType) {
		TaskProcess tp =  taskProcessDao.getById(task.getTpid());
		tp.setHanldresult(task.getHanldresult());
		tp.setHanldtime(DateTools.getCurrentTime());
		tp.setHanldestate(1);
		taskProcessDao.update(tp);
		
		if(attachFile != null && attachFileFileName != null){
			attachManager.saveAttachs(realpath, tp.getHanlder(),"task", ""+task.getTaskid(), attachFile, attachFileFileName, attachFileContentType);
		}
		
		
		if(task.getExecUsers() != null){
			for(Long userid : task.getExecUsers()){
				TaskProcess nextTp = new TaskProcess();
				nextTp.setTaskid(task.getTaskid());
				nextTp.setTasktime(DateTools.getCurrentTime());
		//		判断是否有代理人
				/*TaskAuth taskAuth = taskAuthDao.getById(task.getHanlder());
				if(taskAuth != null){
					nextTp.setHanlder(taskAuth.getAuthid());
					nextTp.setHanldetype(3);
				}else{
					nextTp.setHanlder(task.getHanlder());
					nextTp.setHanldetype(1);
				}*/
				//nextTp.setHanlder(task.getHanlder());
				nextTp.setZpr(tp.getHanlder());
				nextTp.setHanlder(userid);
				nextTp.setHanldetype(1);
				nextTp.setOpttype(task.getOpttype());
				nextTp.setHanldestate(0);
				nextTp.setTaskaction(0);
				taskProcessDao.save(nextTp);
				
		//		保存任务提示
				MsgTips tips = new MsgTips();
				tips.setBt(task.getTitle());
				tips.setContent(task.getTitle());
				tips.setTablename(Notes.task);
				tips.setTableid(task.getTaskid()+"");
				tips.setZt(0);
				tips.setXxsj(DateTools.getCurrentTime());
				/*if(taskAuth != null){
					tips.setUserid(taskAuth.getAuthid());
				}else{
					tips.setUserid(task.getHanlder());
				}*/
				tips.setUserid(userid);
				tips.setUrl(Notes.taskUrl+"taskid="+task.getTaskid());
				msgTipsDao.save(tips);
			}
		}
	}
	
	/**执行处理任务*/
	public void zxclTask(Task task, String realpath, List<File> attachFile, List<String> attachFileFileName, List<String> attachFileContentType) {
		TaskProcess tp =  taskProcessDao.getById(task.getTpid());
		tp.setHanldresult(task.getHanldresult());
		tp.setHanldtime(DateTools.getCurrentTime());
		tp.setHanldestate(1);
		taskProcessDao.update(tp);
		taskProcessDao.flush();
		
		//TaskAuth taskAuth = taskAuthDao.getById(task.getHanlder());
		Integer count = taskProcessDao.getCount("select count(*) from task_process t where t.hanldestate=0 and t.taskid="+task.getTaskid());
		if(count.intValue() == 0){
			TaskProcess nextTp = new TaskProcess();
			nextTp.setTaskid(task.getTaskid());
			nextTp.setTasktime(DateTools.getCurrentTime());
			nextTp.setHanlder(task.getCreater());
			nextTp.setHanldetype(1);
			nextTp.setOpttype(task.getOpttype());
			nextTp.setHanldestate(0);
			nextTp.setTaskaction(2);
			nextTp.setZpr(tp.getHanlder());
			taskProcessDao.save(nextTp);
			
//			保存任务提示
			MsgTips tips = new MsgTips();
			tips.setBt(task.getTitle());
			tips.setContent(task.getTitle());
			tips.setTablename(Notes.task);
			tips.setTableid(task.getTaskid()+"");
			tips.setZt(0);
			tips.setXxsj(DateTools.getCurrentTime());
			tips.setUserid(task.getCreater());
			tips.setUrl(Notes.taskUrl+"taskid="+task.getTaskid());
			msgTipsDao.save(tips);
		}
		if(attachFile != null && attachFileFileName != null){
			attachManager.saveAttachs(realpath, tp.getHanlder(),"task", ""+task.getTaskid(), attachFile, attachFileFileName, attachFileContentType);
		}
	}
	
	/**执行抄送任务处理*/
	public void tjrwTask(Task task, String realpath, List<File> attachFile, List<String> attachFileFileName, List<String> attachFileContentType) {
		TaskProcess tp =  taskProcessDao.getById(task.getTpid());
		tp.setHanldresult(task.getHanldresult());
		tp.setHanldtime(DateTools.getCurrentTime());
		tp.setHanldestate(1);
		taskProcessDao.update(tp);
		
		if(attachFile != null && attachFileFileName != null){
			attachManager.saveAttachs(realpath, tp.getHanlder(),"task", ""+task.getTaskid(), attachFile, attachFileFileName, attachFileContentType);
		}
		
		if(task.getExecUsers() != null){
			for(Long userid : task.getExecUsers()){
				TaskProcess nextTp = new TaskProcess();
				nextTp.setTaskid(task.getTaskid());
				nextTp.setTasktime(DateTools.getCurrentTime());
				nextTp.setHanlder(userid);
				nextTp.setHanldetype(2);
				nextTp.setOpttype(task.getOpttype());
				nextTp.setHanldestate(0);
				nextTp.setTaskaction(3);
				nextTp.setZpr(tp.getHanlder());
				taskProcessDao.save(nextTp);
				
				MsgTips tips = new MsgTips();
				tips.setBt(task.getTitle());
				tips.setContent(task.getTitle());
				tips.setTablename(Notes.task);
				tips.setTableid(task.getTaskid()+"");
				tips.setZt(0);
				tips.setXxsj(DateTools.getCurrentTime());
				tips.setUserid(userid);
				tips.setUrl(Notes.taskUrl+"taskid="+task.getTaskid());
				msgTipsDao.save(tips);
			}
		}
	}

	public Integer closeTask(Task task, String realpath, List<File> attachFiles, List<String> attachFilesFileName, List<String> attachFilesContentType) {
		TaskProcess tp =  taskProcessDao.getById(task.getTpid());
		tp.setHanldresult(task.getHanldresult());
		tp.setHanldtime(DateTools.getCurrentTime());
		tp.setHanldestate(1);
		taskProcessDao.update(tp);
		
		//任务完成后，对抄送部门发送任务提示
		Integer bmws = taskDeptDao.getCount("select count(*) from task_dept t where t.sendtype=2 and t.taskid="+task.getTaskid());
		List<SysUser> listDeptUser = taskDeptDao.searchTaskUserDept(task.getTaskid(),2);
		if(listDeptUser == null){
			return 2;
		}else if(bmws != listDeptUser.size()){
			return 3;
		}else{
			for(SysUser user : listDeptUser){
				TaskProcess nextTp = new TaskProcess();
				nextTp.setTaskid(task.getTaskid());
				nextTp.setTasktime(DateTools.getCurrentTime());
				nextTp.setHanlder(user.getUserid());
				nextTp.setHanldetype(2);
				nextTp.setOpttype(task.getOpttype());
				nextTp.setHanldestate(0);
				nextTp.setTaskaction(3);
				nextTp.setZpr(tp.getHanlder());
				taskProcessDao.save(nextTp);
				
				
				MsgTips tips = new MsgTips();
				tips.setBt(task.getTitle());
				tips.setContent(task.getTitle());
				tips.setTablename(Notes.task);
				tips.setTableid(task.getTaskid()+"");
				tips.setZt(0);
				tips.setUserid(user.getUserid());
				tips.setXxsj(DateTools.getCurrentTime());
				tips.setUrl(Notes.taskUrl+"taskid="+task.getTaskid());
				msgTipsDao.save(tips);
			}
		}
		
		Integer count = taskProcessDao.getCount("select count(*) from task_process t where t.hanldestate=0 and t.taskaction<>2 and t.taskid="+task.getTaskid());
		if(count.intValue() == 0){
			task.setTaskstate(1);
			task.setExecstate(1);
			task.setComplettime(DateTools.getCurrentTime());
			taskDao.update(task);
		}else{
			return 0;
		}
		if(attachFiles != null && attachFilesFileName != null){
			attachManager.saveAttachs(realpath,tp.getHanlder(),"task", ""+task.getTaskid(), attachFiles, attachFilesFileName, attachFilesContentType);
		}
		return 1;
	}

	public Page mySearch(TaskQuery query) {
		return taskDao.mySearch(query);
	}
	/**未处理任务数*/
	public Integer getWclTask(Long userid){
		return taskDao.getCount("select count(*) from task_process t where t.hanldestate=0 and t.hanlder="+userid);
	}
	/**已处理任务数*/
	public Integer getYclTask(Long userid){
		return taskDao.getCount("select count(*) from task_process t where t.hanldestate=1 and t.hanlder="+userid);
	}
	public void tjspTask(Task task, String realpath, List<File> attachFiles, List<String> attachFilesFileName, List<String> attachFilesContentType) {
		task.setTaskstate(0);
		taskDao.update(task);
		
		if(task.getDeptids() != null){
			taskDeptDao.delete("delete task_dept t where t.sendtype=1 and t.taskid="+task.getTaskid());
			for(Long deptid : task.getDeptids()){
				TaskDept td = new TaskDept();
				td.setTaskid(task.getTaskid());
				td.setDeptid(deptid);
				td.setSendtype(1);
				taskDeptDao.save(td);
			}
		}
		
		if(task.getSendDeptids() != null){
			taskDeptDao.delete("delete task_dept t where t.sendtype=2 and t.taskid="+task.getTaskid());
			for(Long deptid : task.getSendDeptids()){
				TaskDept td = new TaskDept();
				td.setTaskid(task.getTaskid());
				td.setDeptid(deptid);
				td.setSendtype(2);
				taskDeptDao.save(td);
			}
		}
		
		TaskProcess tp =  taskProcessDao.getById(task.getTpid());
		tp.setHanldresult(task.getHanldresult());
		tp.setHanldtime(DateTools.getCurrentTime());
		tp.setHanldestate(1);
		tp.setHanldetype(1);//设置办理类型为“转下一审批”
		taskProcessDao.update(tp);
		
//		保存新任务
		TaskProcess nextTp = new TaskProcess();
		nextTp.setTaskid(task.getTaskid());
		nextTp.setTasktime(DateTools.getCurrentTime());
		nextTp.setHanlder(task.getHanlder());
		nextTp.setHanldetype(1);
		nextTp.setOpttype(task.getOpttype());
		nextTp.setHanldestate(0);
		nextTp.setTaskaction(1);
		nextTp.setZpr(tp.getHanlder());
		taskProcessDao.save(nextTp);
		
		if(attachFiles != null && attachFilesFileName != null){
			attachManager.saveAttachs(realpath, tp.getHanlder(),"task", ""+task.getTaskid(), attachFiles, attachFilesFileName, attachFilesContentType);
		}
		
		/**保存任务提示*/
		MsgTips tips = new MsgTips();
		tips.setBt(task.getTitle());
		tips.setContent(task.getTitle());
		tips.setTablename(Notes.task);
		tips.setTableid(task.getTaskid()+"");
		tips.setZt(0);
		tips.setXxsj(DateTools.getCurrentTime());
		tips.setUserid(task.getHanlder());
		tips.setUrl(Notes.taskUrl+"taskid="+task.getTaskid());
		msgTipsDao.save(tips);
	}
}
